package com.ebelter.btcomlib.models.bluetooth.products.three2one;


import android.content.Context;
import android.text.TextUtils;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.bluetooth.base.BaseManager;

import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;

import com.ebelter.btcomlib.models.bluetooth.interfaces.IDataReadWriteCallback;

import com.ebelter.btcomlib.models.bluetooth.products.three2one.interfaces.ThreeOneMesureResult;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

/**
 * 三合一
 */
public class ThreeOneManager extends BaseManager{

    private static final String TAG = "ThreeOneManager";



    private ThreeOneBytesAnalysis mThreeOneBytesAnalysis;
    private ThreeOneBytesMake mThreeOneBytesMake;




    public ThreeOneManager(Context mContext) {
        super(mContext, ProductStyle.THREE_IN_ONE, ThreeOneConstant.ThreeOne_SERVICE_UUID, new String[]{ThreeOneConstant.ThreeOne_WRITE_UUID},
                new String[]{ThreeOneConstant.ThreeOne_READ_UUID1},false,300);

        mThreeOneBytesAnalysis = new ThreeOneBytesAnalysis(mContext);
        mThreeOneBytesMake = new ThreeOneBytesMake();


        mServiceGatt.setIDataReadWriteCallback(new IDataReadWriteCallback() {
            @Override
            public void onCharacteristicChanged(final byte[] data) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtils.i(TAG, "ThreeOneManager--IDataReadWriteCallback-onCharacteristicChanged--读到-" + BytesUtils.bytes2HexStr(data));
                        mThreeOneBytesAnalysis.resultAnalysis(data);
                    }
                });


            }

            @Override
            public void writeData(byte[] data) {
                // LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-writeData--写入-"+ Arrays.toString(data));
            }
        });
    }







    public boolean isBondDevice() {
        String add = getBondDeviceAddress();
        return !TextUtils.isEmpty(add);
    }

    public String getBondDeviceAddress() {
        return SpUtil.readString(ThreeOneConstant.ThreeOne_BOND_DEVICE_ADDRESS, null);
    }

    public void setBondDeviceAddress(String address) {
        SpUtil.writeString(ThreeOneConstant.ThreeOne_BOND_DEVICE_ADDRESS, address);
    }





    public void setThreeOneMeasureCallback(ThreeOneMesureResult threeOneMesureResult) {
        mThreeOneBytesAnalysis.setThreeOneMeasureCallback(threeOneMesureResult);
    }

    //time--->0空腹 1餐前2小时 2餐后2小时 3睡前
    //result--->0良好 1一般 2偏低 3不良
    public static int computerBGResult(int time,float resultValue){
        //0空腹 1餐后
        if (time==0||time==1||time==3){
            if (resultValue<3.9f){
                return 2;
            }else if (resultValue>=3.9f&&resultValue<=6.1f){
                return 0;
            }else if (resultValue>6.1f&&resultValue<=7f){
                return 1;
            }else if (resultValue>7f){
                return 3;
            }
        }else if (time==2){

            if (resultValue<3.9f){
                return 2;
            }else if (resultValue>=3.9f&&resultValue<=8.0f){
                return 0;
            }else if (resultValue>8.0f&&resultValue<=10.0f){
                return 1;
            }else if (resultValue>10.0f){
                return 3;
            }
        }
        return 0;
    }

    public static String computerBGResultDesc(int result){
        if (result==0){
            return getString_(R.string.lianghao);
        }else if (result==1){
            return getString_(R.string.yiban);
        }else if (result==2){
            return getString_(R.string.piandi);
        }else {
            return getString_(R.string.buliang);
        }
    }

    public static String computerUAResultDesc(int result){
        if (result==0){
            return getString_(R.string.zhengchang);
        }
        return getString_(R.string.pianzhong);
    }


    /**
     * @param sex 0男1女
     * @param age 年龄
     * @param resultValue 尿素值
     * @return 0正常 1偏高
     */
    public static int computerUAHResult(int sex,int age,int resultValue){

        if (sex==0){

            if (age<=60){
                if (resultValue>=149&&resultValue<=416){
                    return 0;
                }else if (resultValue>416){
                    return 1;
                }
            }else {
                if (resultValue>=250&&resultValue<=476){
                    return 0;
                }else if (resultValue>476){
                    return 1;
                }
            }


        }else if (sex==1){
            if (age<=60){
                if (resultValue>=89&&resultValue<=357){
                    return 0;
                }else if (resultValue>357){
                    return 1;
                }
            }else {
                if (resultValue>=190&&resultValue<=434){
                    return 0;
                }else if (resultValue>434){
                    return 1;
                }
            }
        }

        return 0;


    }


    /**
     * 总胆固醇 0过低 1正常 2过高
     */
    public static int computerTZHHResult(float resultValue){
        resultValue=NumUtils.numBaoLiuWei2(resultValue);
        if (resultValue<2.86f){
            return 0;
        }else if (resultValue>=2.86f&&resultValue<=5.98f){
            return 1;
        }else {
            return 2;
        }
    }


    public static String computerTZHHResultDesc(int result){

        if (result==0){
            return getString_(R.string.T_guodi);
        }else if (result==1){
            return getString_(R.string.T_zhengchang);
        }else {
            return getString_(R.string.T_guogao);
        }
    }


    public void clear() {
        if (mThreeOneBytesAnalysis!=null)mThreeOneBytesAnalysis.clear();
    }
}
